package sparrowGui.components
{
	import flash.display.DisplayObject;
	import flash.display.DisplayObjectContainer;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.geom.Point;
	
	import sparrowGui.components.item.ItemFactory;
	import sparrowGui.components.item.SListItem;
	import sparrowGui.event.ListEvent;
	import sparrowGui.i.IItem;
	import sparrowGui.i.IItemFactory;
	
	[Event(name="list_item_select", 	type="sparrowGui.event.ListEvent")]
	
	/**
	 * 弹出菜单,类似鼠标右键
	 * 
	 * 例子如下
	 * 
	 * var actBtn:simplyButton;
		actBtn.addEventListener(MouseEvent.CLICK,onBtnClick);
		var pop:SPopupMenu = new SPopupMenu(this);
		pop.addEventListener(ListEvent.LIST_ITEM_SELECT,onListSelect);
		
		function onBtnClick(e:MouseEvent):void
		{
			pop.show([11,22,33,44,55],actBtn);
		}
		function onBtnClick(e:ListEvent):void
		{
			trace(e.target);
		}
	 * 
	 * @author Pelephone
	 * @website http://cnblogs.com/pelephone
	 */
	public class SPopupMenu extends BaseTip
	{
		/**
		 * 列表组件
		 */
		public var list:SRadioGroup;
		
		/**
		 * 构造弹窗菜单组件
		 * @param showParent 列表弹出到的容器
		 * @param uiVars 皮肤变量
		 * @param itemFactory 子项工厂，用于创建子项和子项皮肤，工厂里还有缓存的功能
		 */
		public function SPopupMenu(showParent:DisplayObjectContainer=null
								   ,uiVars:Object=null,itemFactory:IItemFactory=null)
		{
			itemFactory = itemFactory || new ItemFactory(SListItem,"listItem");
			list = new SRadioGroup(uiVars,itemFactory);
			list.colNum = 1;
			
			super(showParent,list.skin);
			
			showPos = TIPS_POSI_AUTO_UP;
		}
		
/*		override protected function create(argSkin:Sprite):void
		{
			
		}*/
		
		override public function update(data:Object):void
		{
			list.update(data as Array);
		}
		
		override public function show(data:Object=null, tarDisp:DisplayObject=null):void
		{
			tarDisp = tarDisp || list;
			super.show(data,tarDisp);
		}
		
		override public function showToTarget(tarDisp:DisplayObject=null):void
		{
			super.showToTarget(tarDisp);
			
			list.addEventListener(ListEvent.LIST_ITEM_SELECT,onSelectEvt);
			showParent.stage.addEventListener(MouseEvent.MOUSE_UP,onMouseUp);
		}
		
		override public function showByPos(posX:Number, posY:Number, data:Object=null):void
		{
			super.showByPos(posX,posY,data);
			
			list.addEventListener(ListEvent.LIST_ITEM_SELECT,onSelectEvt);
			showParent.stage.addEventListener(MouseEvent.MOUSE_UP,onMouseUp);
		}
		
		private function onMouseUp(e:MouseEvent):void
		{
			var clickTars:Array = skinDC.getObjectsUnderPoint(new Point(e.stageX,e.stageY));
			if(clickTars && clickTars.length>0) return;
			clearUp();
		}
		
		private function onSelectEvt(e:Event):void
		{
			dispatchEvent(e);
			clearUp();
		}
		
		override public function clearUp():void
		{
			super.clearUp();
			list.removeEventListener(ListEvent.LIST_ITEM_SELECT,onSelectEvt);
			showParent.stage.removeEventListener(MouseEvent.MOUSE_UP,onMouseUp);
			
			dispatchEvent(new Event(Event.CANCEL));
		}
		
		/**
		 * 选中第X项
		 * @return 
		 */
		public function get selectIndex():int
		{
			return list.getSelectIndex();
		}
		
		/**
		 * 选中项内容
		 * @return 
		 */
		public function get selectItem():IItem
		{
			return list.getSelectItem();
		}
	}
}